<?php
namespace app\api\controller;
use app\service\cxyunPayService;
use app\service\cxyunService;
use app\service\roominfoService;
use app\BaseController;
use think\Exception;
use think\Request;
use think\facade\Db;

class Cxyun extends BaseController{
    public function record(){
        $con=file_get_contents("php://input");
        $data=json_decode($con,true);
        $info["parkingId"]=$data["park_id"];
        $info["parkingNume"]=$data["park_name"];
        $info["recordId"]=$data["record_id"];
        $info["plateNumber"]=$data["plate_number"];
        $info["plateColor"]=$data["plate_color"];
        //查询是否四月租车或福利车
        $carInfo=Db::table("car")
            ->where("parkingId",$info["parkingId"])
            ->where("carType",2)
            ->where("plateNumber",$info["plateNumber"])
            ->find();

        $info["room_id"]=$carInfo["room_id"];
        $info["car_id"]=$carInfo["id"];
        $info["carType"]=$carInfo["carType"];
        if(empty($carInfo)){
            return json(["status"=>1,"message"=>""]);
        }
        //
        $recordInfo=Db::table("cxyun_entry_exit_record")
            ->where("recordId",$info["recordId"])
            ->find();

        //入场
        if($data["is_exit"]=="0"){
            $info["enterReleaseType"]=$data["release_type"];
            $info["enterRemark"]=$data["remark"];
            $info["entranceTime"]=$data["entrance_time"];
            $info["entranceGateId"]=$data["entrancegate_id"];
            $info["entranceGateName"]=$data["entrancegate_name"];
        }else{
            //出场
            $info["exitReleaseType"]=$data["release_type"];
            $info["exitRemark"]=$data["remark"];
            $info["exitGateName"]=$data["exitgate_name"];
            $info["exitGateId"]=$data["exitgate_id"];
            $info["exitTime"]=$data["exit_time"];
        }
        if(empty($recordInfo)){
            $res= Db::table("cxyun_entry_exit_record")->insert($info);
            if($res){
                return json(["status"=>1,"message"=>"入场成功"]);
            }else{
                return json(["status"=>0,"message"=>"入场失败"]);
            }
        }else{
            $res= Db::table("cxyun_entry_exit_record")->where("id",$recordInfo["id"])->update($info);
            if($res){
                return json(["status"=>1,"message"=>"入场2成功"]);
            }else{
                return json(["status"=>0,"message"=>"入场2失败"]);
            }
        }
    }

    
    public function paid(){
        $path=dir(getcwd())->path.'/cxyun/'.date('Ymd')."/pay";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        $con=file_get_contents($address);
        //$con=file_get_contents("php://input");
        $data=json_decode($con,true);
        //查询停车场信息
        $parkinglotinfo=Db::table("parkinglotinfo")->where("cxyun_parking_id",$data["parkId"])->find();

        $info["parkingId"]=$data["parkId"];
        $info["plateNumber"]=$data["carNo"];

        $info["recordId"]=$data["onlyCode"];
        $info["amount"]=$data["amount"];
        $info["cartypeName"]=$data["carType"];
        $info["carmodelName"]=$data["carSize"];
        $info["inLaneCode"]=$data["inLaneCode"];
        $info["outLaneCode"]=$data["outLaneCode"];
        //


        $result=(new cxyunPayService())->getCarOrderMoney($info,$parkinglotinfo["cxyun_appkey"]);
        if($result["status"]==0){
            return $result;
        }
        $result= json_decode($result["Data"],true);
        $payNotice["parkingId"]=$info["parkingId"];
        $payNotice["plateNumber"]=$info["plateNumber"];
        $payNotice["payFee"]=$info["amount"];
        $payNotice["orderNo"]=$result["orderNo"];
        $payNotice["payType"]=5;
        $payNotice["timestamp"]=time()."000";

        $info["orderNo"]=$result["orderNo"];
        $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
        //改订单是否支付
        $payInfo=(new cxyunPayService())->getPayInfo($info);

        if(empty($payInfo)){
            //查询是否是月租车或福利车
            $carInfo=(new cxyunPayService())->getWelfareCarInfo($info);

            $info["room_id"]=$carInfo["room_id"];
            $info["car_id"]=$carInfo["id"];
            $info["carType"]=$carInfo["carType"];
            if(!empty($carInfo)){
                //查询房屋金额
                $roominfo=(new roominfoService())->getRoomInfo($carInfo["room_id"]);
                if(empty($roominfo)){
                    return json(["status"=>0,"message"=>"房屋号不存在,请重新绑定车牌"]);
                }
                if($roominfo["welfare_car_balance"]<$info["amount"]){
                    return json(["status"=>0,"message"=>"余额不足"]);
                }
                //福利车 扣除房屋福利车金额
                $result=(new cxyunPayService())->welfareCarPay($info,$roominfo);
                if($result["code"]==0){
                    $payNotice["orderId"]=$result["billid"];

                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                    //var_dump($res);
                    return json(["status"=>1,"message"=>"支付成功"]);
                }else{
                    return json(["status"=>500,"message"=>$result["msg"]]);
                }
//                if($carInfo["carType"]==1){
//                    if($roominfo["monthly_car_balance"]<$info["amount"]){
//                        return json(["status"=>0,"message"=>"余额不足"]);
//                    }
//                    $result=(new cxyunPayService())->monthlyCarPay($info,$roominfo);
//                    if($result["code"]==0){
//                        $payNotice["orderId"]=$result["billid"];
//
//                        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
//                        var_dump($res);
//                        return json(["status"=>1,"message"=>"支付成功"]);
//                    }else{
//                        return json(["status"=>500,"message"=>$result["msg"]]);
//                    }
//                }else
                // if($carInfo["carType"]==2){
                //     if($roominfo["welfare_car_balance"]<$info["amount"]){
                //         return json(["status"=>0,"message"=>"余额不足"]);
                //     }
                //     //福利车 扣除房屋福利车金额
                //     $result=(new cxyunPayService())->welfareCarPay($info,$roominfo);
                //     if($result["code"]==0){
                //         $payNotice["orderId"]=$result["billid"];

                //         $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                //         var_dump($res);
                //         return json(["status"=>1,"message"=>"支付成功"]);
                //     }else{
                //         return json(["status"=>500,"message"=>$result["msg"]]);
                //     }
                // }
            }

        }else{
            $payNotice["orderId"]=$payInfo["id"];
            $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
            //var_dump($res);
            return json(["status"=>1,"message"=>"成功"]);;
        }
    }

    public function payfree(){
        $path=dir(getcwd())->path.'/pay/'.date('Ymd')."/cxyunpayfree";
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        $con=file_get_contents($address);
        //$con=file_get_contents("php://input");
        $data=json_decode($con,true);
        $info["parkingId"]=$data["parkId"];
        $info["plateNumber"]=$data["carNo"];

        $info["recordId"]=$data["onlyCode"];
        $info["amount"]=$data["amount"];
        $info["cartypeName"]=$data["carType"];
        $info["carmodelName"]=$data["carSize"];
        $info["inLaneCode"]=$data["inLaneCode"];
        $info["outLaneCode"]=$data["outLaneCode"];
        //
        $result=(new cxyunPayService())->getCarOrderMoney($info,'');
        if($result["status"]==0){
            return $result;
        }
        $result= json_decode($result["Data"],true);
        $payNotice["parkingId"]=$info["parkingId"];
        $payNotice["plateNumber"]=$info["plateNumber"];
        $payNotice["payFee"]=$info["amount"];
        $payNotice["orderNo"]=$result["orderNo"];
        $payNotice["payType"]=262;
        $payNotice["timestamp"]=time()."000";

        $info["orderNo"]=$result["orderNo"];
        $cxyunApi=new cxyunService();
        //改订单是否支付
        $payInfo=(new cxyunPayService())->getPayInfo($info);

        if(empty($payInfo)){
            //查询是否是月租车或福利车
            $carInfo=(new cxyunPayService())->getWelfareCarInfo($info);

            $info["room_id"]=$carInfo["room_id"];
            $info["car_id"]=$carInfo["id"];
            $info["carType"]=$carInfo["carType"];
            if(!empty($carInfo)){
                $payNotice["orderId"]=$payNotice["orderNo"];
                $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                //var_dump($res);
                return json(["status"=>1,"message"=>"支付成功"]);
            }

        }else{
            $payNotice["orderId"]=$payInfo["id"];
            $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
            //var_dump($res);
            return json(["status"=>1,"message"=>"成功"]);;
        }
    }


    public function payrecord(){
        //file_get_contents("php://input");
        $path=dir(getcwd())->path.'/pay/'.date('Ymd')."/cxyunpayrecord";
        //$path=dir(getcwd())->path.'/pay/cxyunpay/'.date('Ymd');
        if (! file_exists ( $path )) {
            mkdir ( "$path", 0777, true );
        }
        $address = $path."/".date('His').rand(100000,999999).'.txt';
        file_put_contents($address,file_get_contents("php://input"),FILE_APPEND);
        
        return json(["status"=>1,"message"=>""]);
    }
    
    
    
    public function test(){
        $path=dir(getcwd())->path.'/cxyun/20240519/pay';
        $address = $path."/172308766572.txt";
        $con=file_get_contents($address);
        $data=json_decode($con,true);
        $parkinglotinfo=Db::table("parkinglotinfo")->where("cxyun_parking_id",$data["parkId"])->find();

        $info["parkingId"]=$data["parkId"];
        $info["plateNumber"]=$data["carNo"];

        $info["recordId"]=$data["onlyCode"];
        $info["amount"]=$data["amount"];
        $info["cartypeName"]=$data["carType"];
        $info["carmodelName"]=$data["carSize"];
        $info["inLaneCode"]=$data["inLaneCode"];
        $info["outLaneCode"]=$data["outLaneCode"];
        //


        $result=(new cxyunPayService())->getCarOrderMoney($info,$parkinglotinfo["cxyun_appkey"]);
        if($result["status"]==0){
            return $result;
        }
        $result= json_decode($result["Data"],true);
        $payNotice["parkingId"]=$info["parkingId"];
        $payNotice["plateNumber"]=$info["plateNumber"];
        $payNotice["payFee"]=$info["amount"];
        $payNotice["orderNo"]=$result["orderNo"];
        $payNotice["payType"]=5;
        $payNotice["timestamp"]=time()."000";

        $info["orderNo"]=$result["orderNo"];
        $cxyunApi=new cxyunService($parkinglotinfo["cxyun_appkey"]);
        //改订单是否支付
        $payInfo=(new cxyunPayService())->getPayInfo($info);

        if(empty($payInfo)){
            //查询是否是月租车或福利车
            $carInfo=(new cxyunPayService())->getWelfareCarInfo($info);

            $info["room_id"]=$carInfo["room_id"];
            $info["car_id"]=$carInfo["id"];
            $info["carType"]=$carInfo["carType"];
            if(!empty($carInfo)){
                //查询房屋金额
                $roominfo=(new roominfoService())->getRoomInfo($carInfo["room_id"]);
                if(empty($roominfo)){
                    return json(["status"=>0,"message"=>"房屋号不存在,请重新绑定车牌"]);
                }
                if($roominfo["welfare_car_balance"]<$info["amount"]){
                    return json(["status"=>0,"message"=>"余额不足"]);
                }
                //福利车 扣除房屋福利车金额
                $result=(new cxyunPayService())->welfareCarPay($info,$roominfo);
                if($result["code"]==0){
                    $payNotice["orderId"]=$result["billid"];

                    $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                    //var_dump($res);
                    return json(["status"=>1,"message"=>"支付成功"]);
                }else{
                    return json(["status"=>500,"message"=>$result["msg"]]);
                }
//                if($carInfo["carType"]==1){
//                    if($roominfo["monthly_car_balance"]<$info["amount"]){
//                        return json(["status"=>0,"message"=>"余额不足"]);
//                    }
//                    $result=(new cxyunPayService())->monthlyCarPay($info,$roominfo);
//                    if($result["code"]==0){
//                        $payNotice["orderId"]=$result["billid"];
//
//                        $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
//                        var_dump($res);
//                        return json(["status"=>1,"message"=>"支付成功"]);
//                    }else{
//                        return json(["status"=>500,"message"=>$result["msg"]]);
//                    }
//                }else
                // if($carInfo["carType"]==2){
                //     if($roominfo["welfare_car_balance"]<$info["amount"]){
                //         return json(["status"=>0,"message"=>"余额不足"]);
                //     }
                //     //福利车 扣除房屋福利车金额
                //     $result=(new cxyunPayService())->welfareCarPay($info,$roominfo);
                //     if($result["code"]==0){
                //         $payNotice["orderId"]=$result["billid"];

                //         $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
                //         var_dump($res);
                //         return json(["status"=>1,"message"=>"支付成功"]);
                //     }else{
                //         return json(["status"=>500,"message"=>$result["msg"]]);
                //     }
                // }
            }

        }else{
            $payNotice["orderId"]=$payInfo["id"];
            $res=$cxyunApi->commonPost("payTempCost",$payNotice,array());
            //var_dump($res);
            return json(["status"=>1,"message"=>"成功"]);;
        }
    }
}